package com.myj.jim.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myj.jim.domain.Operator;
import com.myj.jim.domain.OperatorRole;
import com.myj.jim.domain.OperatorRoleAuthority;
import com.myj.jim.domain.ResultMessage;
import com.myj.jim.domain.bootstraptable.BootstrapTable;
import com.myj.jim.domain.bootstraptable.BootstrapTableJSON;
import com.myj.jim.service.MenuService;
import com.myj.jim.service.OperatorService;
import com.myj.jim.service.SysLogService;
import com.myj.jim.util.BootstrapTableUtil;
import com.myj.jim.util.JimUtil;

@Controller
@RequestMapping("/operator")
public class OperatorController {

	private static final String PAGE_AUTHORITY = "operatorManage";
	
	private static final String NO_AUTHORITY_VIEW = "common/noAuthorityPage";
	
	@Autowired
	private OperatorService operatorService;
	
	@Autowired
	private SysLogService sysLogService;
	
	@Autowired
	private MenuService menuService;
	
	@RequestMapping("/list")
	@ResponseBody
	private BootstrapTableJSON<Operator> list(BootstrapTable bt){
		
		Map<String,Object> map = BootstrapTableUtil.getParamMap(bt);
		
		return operatorService.findPage(map);
	}
	
	@RequestMapping("/page")
    public String toIndex2(Model model, HttpSession session, HttpServletRequest req){
		String path = req.getContextPath();
		String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
		Operator op = (Operator)session.getAttribute("operator");
		if(op!=null){
			if(JimUtil.haveAuthority(op, PAGE_AUTHORITY)){
				return "operator/page";
			}else{
				return NO_AUTHORITY_VIEW;
			}
			
		}else{
			//到登录页面
			return "redirect:"+basePath+"index.jsp";
		}
    	
    }
	
	@RequestMapping("/save")
	@ResponseBody
	public Boolean save(Operator op, String roleSelectionsJson, HttpServletRequest req,HttpSession session){
		
		Operator sysOp = (Operator)session.getAttribute("operator");
		
		
		boolean success = operatorService.save(op, roleSelectionsJson);
		if(success){
			if(sysOp!=null){
				JimUtil.insertLog(sysLogService, req, op, "10002", op.getLoginid(), op.getName(), "");
			}
			
		}
		return success;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Boolean update(Operator op, String roleSelectionsJson, HttpServletRequest req,HttpSession session){
		Operator sysOp = (Operator)session.getAttribute("operator");
		boolean success = operatorService.update(op, roleSelectionsJson);
		if(success){
			JimUtil.insertLog(sysLogService, req, sysOp, "10003", op.getLoginid(), op.getName(), "");
		}
		return success;
	}
	
	@RequestMapping("/isExist")
	@ResponseBody
	public Boolean isExist(String loginid){
		return operatorService.isExist(loginid);
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Boolean delete(Integer[] idsArr, HttpServletRequest req,HttpSession session){
		Operator sysOp = (Operator)session.getAttribute("operator");
		List<Operator> ops = operatorService.getOperatorListByIds(idsArr);
		
		operatorService.delete(idsArr);
		
		for(Operator op : ops){
			JimUtil.insertLog(sysLogService, req, sysOp, "10004", op.getLoginid(), op.getName(), "");
		}
		return true;
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public ResultMessage login(Operator op, HttpSession session, HttpServletRequest req){
		
		op = operatorService.login(op);

		ResultMessage rm = new ResultMessage();
		if(op!=null){
			if(op.getFlag()==1){
				
				List<OperatorRoleAuthority> authList = operatorService.selectOperatorRoleAuthorityByLoginid(op.getLoginid());
				
				Map<String, Object> authMap = new HashMap<String, Object>();
				for(OperatorRoleAuthority opa : authList){
					authMap.put(opa.getAuthorityno(), opa.getAuthorityno());
				}
				op.setAuthorityMap(authMap);
				
				session.setAttribute("operator", op);
				session.setAttribute("menuList", menuService.getMenuList());
				rm.setSuccess(true);
				rm.setMessage("登录成功");
				
				JimUtil.insertLog(sysLogService, req, op, "10001", "", "", "");
			}else{
				rm.setSuccess(false);
				rm.setMessage("该用户不可用，请联系管理员");
			}
		}else{
			rm.setSuccess(false);
			rm.setMessage("用户名或密码错误");
		}
		
		return rm;
	}
	
	@RequestMapping("/index")
	public String index(){
		return "index";
	}
	
	
	@RequestMapping("/getRoleListByLoginid")
	@ResponseBody
	public List<OperatorRole> getRoleListByLoginid(String loginid){
		return operatorService.getRoleListByLoginid(loginid);
	}
	
	@RequestMapping("logout")
	public String logout(HttpServletRequest req, HttpSession session){
		String path = req.getContextPath();
		String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
		
		session.removeAttribute("operator");
		
		return "redirect:"+basePath+"index.jsp";
	}
}
